!(function(e) {
    "use strict";
    const lanui1 = "color: #fff;background-color: green;padding:4px 10px;border-top-left-radius: 4px;border-bottom-left-radius: 4px";
    const lanui2 = "color: #fff;background-color: gray;padding:4px 10px;border-top-right-radius: 4px;border-bottom-right-radius: 4px";
    console.log("%clanui-drift %c1.00", lanui1, lanui2);
    var r = function() {},
        drift = function() {}; //漂流
    r.prototype.drift = drift; //漂流

    //漂流开始
    drift.prototype.init = function(option) { //漂流初始化
        var _option = {
            el: "#drift", //漂流父级元素
            width: "100%", //漂流的宽度
            event: "click", //箭头触发事件
            height: 240, //漂流的高度
            space: false, //进度条到边的距离
            loop: true, //是否循环
            padding: [0, 0, 0, 0], //内边距
            margin: [0, 0, 0, 0], //外边距
            progress: true, //是否有进度条
            pPosition: "bottom", //进度条位置 left top right bottom
            pPSpace: 20, //进度条位置到边上的距离
            pSpace: 5, //进度条间距
            pWidth: 10, //进度条宽度    
            pHeight: 5, //进度条高度    
            pRadius: 0, //圆角    
            pDColor: "#ffffff", //进度条默认颜色    
            pHColor: "#cccccc", //进度条hover颜色    
            pAColor: "#ff007f", //进度条焦点状态颜色    
            pAColor: "#ff007f", //进度条焦点状态颜色    
            pARadius: 0, //进度条焦点状态圆角   
            pAWidth: 20, //进度条焦点状态宽度   
            pAHeight: 5, //进度条焦点状态高度  
            screenLiLength: 1, //屏幕显示的条数   
            arrow: true //方向箭头 
        };
        var _opt = {
            li: null, //轮播图的节点
            length: 0, //轮播图的节点长度
            s: 0, //移动的长度
            x: 0, //开始移动的x坐标
            xleft: [], //开始移动前的left
            active: 0, //当前进度索引
            pli: "", //进度节点
            screenWidth: document.body.clientWidth, //轮播图宽度
            screenMove: document.body.clientWidth, //轮播图移动的距离
            index: 0, //索引
            cLength: 0, //屏幕中间的列
            touchS: 0, //触碰开始时间
            touchE: 0, //触碰结束时间
            padddingHtml: 0, //内边距字符串
            marginHtml: 0, //内边距字符串
            t: 0
        };
        _option = Object.assign({}, _option, option);
        console.log(_option);
        _opt.t++;
        //轮播图移动的距离
        _opt.screenMove = _opt.screenWidth / _option.screenLiLength
            //设置轮播图样式
        $(_option.el).addClass("lanui-drift");
        $(_option.el + ">ul").addClass("lanui-drift-ul");
        $(_option.el + ">ul>li").addClass("lanui-drift-li");
        _opt.padddingHtml = _option.padding.toString().replace(/,/g, "px ") + "px";
        _opt.marginHtml = _option.margin.toString().replace(/,/g, "px ") + "px";
        console.log(_opt.padddingHtml);
        console.log(_opt.marginHtml);
        $(_option.el).css({ width: _option.width == "100%" ? _opt.screenWidth : _option.width, height: _option.height, padding: _opt.padddingHtml, margin: _opt.marginHtml });
        $(_option.el + ">ul>li").css({ width: _option.width == "100%" ? _opt.screenWidth / _option.screenLiLength : _option.width / _option.screenLiLength, height: _option.height });
        //设置轮播图节点
        _opt.li = $(_option.el + " li");
        //设置轮播图节点的长度
        _opt.length = _opt.li.length;
        //设置ul的宽度
        $(_option.el + ">ul").css({ width: _opt.screenWidth * _opt.length });
        //循环则设置
        if (_option.loop) {

        } else {

        };
        //进度条样式设置
        if (_option.progress) {
            let _li = ``;
            for (let i = 0; i < $(_option.el + " li").length - _option.screenLiLength + 1; i++) {
                if (i == 0) {
                    if (_option.pPosition == "left" || _option.pPosition == "right") {
                        if (_option.pPosition == "right")
                            _li += "<li class='lanui-drift-active' style='width:" + _option.pWidth + "px;border-radius:" + _option.pRadius + "px;height:" + _option.pHeight + "px;margin-bottom:" + _option.pSpace + "px;display:block;margin-left:" + (_option.pAWidth - _option.pWidth) + "px'></li>";
                        else
                            _li += "<li class='lanui-drift-active' style='width:" + _option.pWidth + "px;border-radius:" + _option.pRadius + "px;height:" + _option.pHeight + "px;margin-bottom:" + _option.pSpace + "px;display:block'></li>";
                    } else {
                        _li += "<li class='lanui-drift-active' style='width:" + _option.pWidth + "px;border-radius:" + _option.pRadius + "px;height:" + _option.pHeight + "px;margin-right:" + _option.pSpace + "px'></li>";
                    }
                } else {
                    if (_option.pPosition == "left" || _option.pPosition == "right") {
                        if (_option.pPosition == "right")
                            _li += "<li style='width:" + _option.pWidth + "px;border-radius:" + _option.pRadius + "px;height:" + _option.pHeight + "px;margin-bottom:" + _option.pSpace + "px;display:block;margin-left:" + (_option.pAWidth - _option.pWidth) + "px'></li>";
                        else
                            _li += "<li style='width:" + _option.pWidth + "px;border-radius:" + _option.pRadius + "px;height:" + _option.pHeight + "px;margin-bottom:" + _option.pSpace + "px;display:block'></li>";
                    } else {
                        _li += "<li style='width:" + _option.pWidth + "px;border-radius:" + _option.pRadius + "px;height:" + _option.pHeight + "px;margin-right:" + _option.pSpace + "px'></li>";

                    }
                }
            }
            //console.log($(_option.el + " li").length);
            //console.log(_li);
            $(_option.el).append("<div class='lanui-drift-progress' style=" + progressPosition(_option) + "><ul>" + _li + "</ul></div>");
        };

        //箭头添加
        if (_option.arrow) {
            $(_option.el).append("<div class='lanui-drift-left'></div>"); //左箭头
            $(_option.el).append("<div class='lanui-drift-right'></div>"); //右箭头
        };

        //进度条样式添加
        if (!$("style[name=progress]").length) {
            $("head").append("<style name='progress'>.lanui-drift-progress li { display: inline-block;background-color: " + _option.pDColor + "}" +
                ".lanui-drift-progress li:hover {display: inline-block;background-color: " + _option.pHColor + "}" +
                ".lanui-drift-progress li.lanui-drift-active {margin-left:0px !important;background-color: " + _option.pAColor + ";border-radius:" + _option.pARadius + "px;width:" + _option.pAWidth + "px !important;height:" + _option.pAHeight + "px !important;border-radius:" + _option.pARadius + "px !important}<style>");
        };
        //轮播移动
        $(document).on({
            touchstart: function(e) {
                _opt.index++;
                //console.log(this)
                //console.log(_opt.index)
                e.stopPropagation(); //禁止冒泡
                // console.log($(this).hasClass("move"))
                if ($(this).hasClass("move"))
                    return;
                _opt.x = e.originalEvent.touches[0].clientX;
                _opt.xleft = $(this).offset().left; //获取每个元素的开始位置
                //console.log(_opt.x);
                _opt.pli = $(_option.el + " .lanui-drift-progress li"); //获取进度节点
                _opt.touchstrat = new Date().getTime();
            },
            touchmove: function(e) {
                e.stopPropagation(); //禁止冒泡
                if ($(this).hasClass("move"))
                    return;
                _opt.s = e.originalEvent.touches[0].clientX - _opt.x; //计算移动的长度

                touchmove(_option, _opt, _opt.s); //设置元素移动中的位置
            },
            touchend: function(e) {
                e.stopPropagation(); //禁止冒泡
                $("#time").val(_opt.s);
                //console.log($(this).hasClass("move"));
                if ($(this).hasClass("move") || _opt.s == 0) {
                    return;
                }
                $(this).addClass("move");
                //touchreset(_opt.s); //重置元素移动前的位置
                touchset(_option, _opt, _opt.s); //设置元素移动后的位置

                _opt.s = 0;
                //console.log("touchend")
            }
        }, _option.el + ">ul");

        //轮播移动
        $(document).on({
            touchstart: function(e) {
                _opt.touchS = e.originalEvent.touches[0].clientX;
            },
            touchmove: function(e) {
                _opt.touchE = _opt.touchstrat - e.originalEvent.touches[0].clientX;
            },
            touchend: function(e) {
                if (!_opt.s && $(this).data("url")) {
                    location = $(this).data("url");
                }
            }
        }, _option.el + ">ul>li");

        //右箭头
        $(document).on(_option.event, _option.el + " .lanui-drift-right", function() {
            if ($(_option.el + ">ul").hasClass("move")) {
                return;
            }
            //console.log(_opt.x);
            _opt.pli = $(_option.el + " .lanui-drift-progress li"); //获取进度节点
            let _L = $(option.el + " li").length - option.screenLiLength + 1;
            $(_option.el + ">ul").addClass("move");
            _opt.xleft = $(_option.el + ">ul").offset().left; //获取ul开始位置
            _opt.active++;
            _opt.active = _opt.active > 0 ? _opt.active - 1 : 0;
            //重置元素移动前的位置
            touchset(_option, _opt, -_opt.screenMove); //设置元素移动后的位置
            _opt.s = 0;
        });

        //左箭头
        $(document).on(_option.event, _option.el + " .lanui-drift-left", function() {
            if ($(_option.el + ">ul").hasClass("move")) {
                return;
            }
            //console.log(_opt.x);
            _opt.pli = $(_option.el + " .lanui-drift-progress li"); //获取进度节点
            $(_option.el + ">ul").addClass("move");
            _opt.active--;
            _opt.xleft = $(_option.el + ">ul").offset().left; //获取ul开始位置
            _opt.active = _opt.active < _opt.length - _option.screenLiLength ? ++_opt.active : _opt.length - _option.screenLiLength;
            //重置元素移动前的位置
            touchset(_option, _opt, _opt.screenMove); //设置元素移动后的位置
            _opt.s = 0;
        });
        //进度条点击
        $(document).on(_option.event, _option.el + " .lanui-drift-progress ul li", function() {
            if ($(_option.el + ">ul").hasClass("move")) {
                return;
            }
            $(_option.el + ">ul").addClass("move");
            _opt.active = $(this).index() - 1;
            _opt.xleft = $(_option.el + ">ul").offset().left; //获取ul开始位置
            _opt.pli = $(_option.el + " .lanui-drift-progress li"); //获取进度节点

            //重置元素移动前的位置
            touchset(_option, _opt, $(this).index() > $(_option.el + " li.lanui-drift-active").index() ? -_opt.screenMove : _opt.screenMove); //设置元素移动后的位置
            _opt.s = 0;
        });

    };


    //返回进度条的位置
    function progressPosition(option) {
        let _space = "";
        let _str = "";
        let _L = $(option.el + " li").length - option.screenLiLength + 1;
        //计算进度条的宽度/高度
        let _pW = option.pPosition == "left" || option.pPosition == "right" ? option.pAWidth : (_L - 1) * (option.pWidth + option.pSpace) + option.pAWidth;
        let _pH = option.pPosition == "left" || option.pPosition == "right" ? (_L - 1) * (option.pHeight + option.pSpace) + option.pAHeight : 0;
        switch (option.pPosition) {
            case "left":
                _space = !option.space ? "top:50%;margin-top:-" + _pH / 2 + "px;" : "top:" + option.space + "px";
                _str = "left:" + (option.pPSpace + option.pHeight) + "px;" + _space + ";height:" + _pH + "px;line-height:" + _pH + "px;width:" + _pW + "px;text-align:left";
                break;
            case "right":
                _space = !option.space ? "top:50%;margin-top:-" + _pH / 2 + "px;" : "top:" + option.space + "px";;
                _str = "right:" + (option.pPSpace + option.pHeight) + "px;" + _space + ";height:" + _pH + "px;line-height:" + _pH + "px;width:" + _pW + "px;text-align:right";
                break;
            case "bottom":
                _space = !option.space ? "left:50%;margin-left:-" + _pW / 2 + "px" : "left:" + option.space + "px";;
                _str = "bottom:" + (option.pPSpace + option.pHeight) + "px;" + _space + ";height:" + _pH + "px;line-height:" + _pH + "px";
                break;
            case "top":
                _space = !option.space ? "left:50%;margin-left:-" + _pW / 2 + "px" : "left:" + option.space + "px";;
                _str = "top:" + (option.pPSpace + option.pHeight) + "px;" + _space + ";height:" + _pH + "px;line-height:" + _pH + "px";
                break;
        };
        return _str;
    };

    //设置元素移动后的位置
    function touchset(_option, _opt, param) {
        console.log(_option.el)
        var _i = 1;
        param = param > _opt.screenMove ? _opt.screenMove : param;
        if (param > 0) {
            if (_opt.xleft + param > 0) {
                $(_option.el + ">ul").animate({ left: 0 }, 400, function() {
                    console.log("右");
                    $(_option.el + ">ul").removeClass("move"); //删除禁止移动样式
                    _opt.active = _opt.active > 0 ? _opt.active - 1 : 0;
                    touchprogress(_opt.active, _opt, _opt.active);
                });
            } else {
                $(_option.el + ">ul").animate({ left: _opt.xleft + _opt.screenMove }, 300, function() {
                    console.log("右");

                    $(_option.el + ">ul").removeClass("move"); //删除禁止移动样式
                    _opt.active -= _i;
                    touchprogress(_opt.active, _opt, _opt.active);
                });
            }
            _opt._s = 0;
        } else {
            // console.log(_opt.xleft - (_opt.screenMove * _i - Math.abs(param)));
            // console.log(-_opt.screenMove * (_opt.length - 1));
            if (_opt.xleft - Math.abs(param) < -_opt.screenMove * (_opt.length - _option.screenLiLength)) {
                $(_option.el + ">ul").animate({ left: -_opt.screenMove * (_opt.length - _option.screenLiLength) }, 400, function() {
                    console.log("左");
                    $(_option.el + ">ul").removeClass("move"); //删除禁止移动样式
                    _opt.active = _opt.active < _opt.length - _option.screenLiLength ? ++_opt.active : _opt.length - _option.screenLiLength;
                    touchprogress(_opt.active, _opt, _opt.active);
                });
            } else {
                $(_option.el + ">ul").animate({ left: _opt.xleft - _opt.screenMove }, 300, function() {
                    console.log("左");
                    $(_option.el + ">ul").removeClass("move"); //删除禁止移动样式
                    _opt.active += _i;
                    touchprogress(_opt.active, _opt, _opt.active);
                });
            }
            _opt._s = 0;
        }
    };

    //设置元素移动中的位置
    function touchmove(_option, _opt, param) {
        //console.log(param);
        $(_option.el + ">ul").css("left", _opt.xleft + param); //设置每个元素的位置
    };

    //重置元素移动前的位置
    function touchreset(_option, _opt, param) {
        $(_option.el + ">ul").animate({ left: _opt.xleft }); //返回起始点
    };

    //设置进度条的样式
    function touchprogress(_option, _opt, param) {
        console.log(param);
        //console.log(_opt.pli);
        //设置进度条的样式
        _opt.pli.removeClass("lanui-drift-active"); //删除当前进度样式
        _opt.pli.eq(param).addClass("lanui-drift-active"); //添加当前进度样式
    };
    //漂流开始结束

    e.lanui = new r;
}(window));